<h1 id="git">git</h1>
<p>是目前世界上最先进的分布式版本控制系统</p>
<h2 id="补充说明">补充说明</h2>
<p><strong>git命令</strong> 很多人都知道，Linus在1991年创建了开源的Linux，从此，Linux系统不断发展，已经成为最大的服务器系统软件了。</p>
<p>Linus虽然创建了Linux，但Linux的壮大是靠全世界热心的志愿者参与的，这么多人在世界各地为Linux编写代码，那Linux的代码是如何管理的呢？</p>
<p>事实是，在2002年以前，世界各地的志愿者把源代码文件通过diff的方式发给Linus，然后由Linus本人通过手工方式合并代码！</p>
<p>你也许会想，为什么Linus不把Linux代码放到版本控制系统里呢？不是有CVS、SVN这些免费的版本控制系统吗？因为Linus坚定地反对CVS和SVN，这些集中式的版本控制系统不但速度慢，而且必须联网才能使用。有一些商用的版本控制系统，虽然比CVS、SVN好用，但那是付费的，和Linux的开源精神不符。</p>
<p>不过，到了2002年，Linux系统已经发展了十年了，代码库之大让Linus很难继续通过手工方式管理了，社区的弟兄们也对这种方式表达了强烈不满，于是Linus选择了一个商业的版本控制系统BitKeeper，BitKeeper的东家BitMover公司出于人道主义精神，授权Linux社区免费使用这个版本控制系统。</p>
<p>安定团结的大好局面在2005年就被打破了，原因是Linux社区牛人聚集，不免沾染了一些梁山好汉的江湖习气。开发Samba的Andrew试图破解BitKeeper的协议（这么干的其实也不只他一个），被BitMover公司发现了（监控工作做得不错！），于是BitMover公司怒了，要收回Linux社区的免费使用权。</p>
<p>Linus可以向BitMover公司道个歉，保证以后严格管教弟兄们，嗯，这是不可能的。实际情况是这样的：</p>
<p>Linus花了两周时间自己用C写了一个分布式版本控制系统，这就是Git！一个月之内，Linux系统的源码已经由Git管理了！牛是怎么定义的呢？大家可以体会一下。</p>
<p>Git迅速成为最流行的分布式版本控制系统，尤其是2008年，GitHub网站上线了，它为开源项目免费提供Git存储，无数开源项目开始迁移至GitHub，包括jQuery，PHP，Ruby等等。</p>
<p>历史就是这么偶然，如果不是当年BitMover公司威胁Linux社区，可能现在我们就没有免费而超级好用的Git了。</p>
<p><a href="https://github.com/jaywcjlove/handbook/blob/master/other/Git%E5%B8%B8%E7%94%A8%E5%91%BD%E4%BB%A4%E6%B8%85%E5%8D%95.md">Git常用命令清单</a></p>
<h3 id="语法">语法</h3>
<pre><code class="language-bash">git [--version] [--help] [-C &lt;path&gt;] [-c name=value]
   [--exec-path[=&lt;path&gt;]] [--html-path] [--man-path] [--info-path]
   [-p | --paginate | --no-pager] [--no-replace-objects] [--bare]
   [--git-dir=&lt;path&gt;] [--work-tree=&lt;path&gt;] [--namespace=&lt;name&gt;]
   &lt;command&gt; [&lt;args&gt;]</code></pre>
<h3 id="选项">选项</h3>
<pre><code class="language-bash">add              将文件内容添加到索引
bisect           通过二进制查找引入错误的更改
branch           列出，创建或删除分支
checkout         检查分支或路径到工作树
clone            将存储库克隆到新目录中
commit           将更改记录到存储库
diff             显示提交，提交和工作树等之间的更改
fetch            从另一个存储库下载对象和引用
grep             打印匹配图案的行
init             创建一个空的Git仓库或重新初始化一个现有的
log              显示提交日志
merge            加入两个或更多的开发历史
mv               移动或重命名文件，目录或符号链接
pull             从另一个存储库或本地分支获取并合并
push             更新远程引用以及相关对象
rebase           转发端口本地提交到更新的上游头
reset            将当前HEAD复位到指定状态
rm               从工作树和索引中删除文件
show             显示各种类型的对象
status           显示工作树状态
tag              创建，列出，删除或验证使用GPG签名的标签对象</code></pre>
<h3 id="例子">例子</h3>
<p>init</p>
<p><code>git init</code> #初始化</p>
<p>status</p>
<p><code>git status</code> #获取状态</p>
<p>add</p>
<p><code>git add file</code> # .或*代表全部添加<br />
<code>git rm --cached &lt;added_file_to_undo&gt;</code> # 在commit之前撤销git add操作<br />
<code>git reset head</code> # 好像比上面<code>git rm --cached</code>更方便</p>
<p>commit</p>
<p><code>git commit -m &quot;message&quot;</code> #此处注意乱码</p>
<p>remote</p>
<p><code>git remote add origin git@github.com:JSLite/test.git</code> #添加源</p>
<p>push</p>
<div class="sourceCode" id="cb3"><pre><code class="language-bash"><a class="sourceLine" id="cb3-1" data-line-number="1"><span class="fu">git</span> push -u origin master <span class="co"># push同事设置默认跟踪分支  </span></a>
<a class="sourceLine" id="cb3-2" data-line-number="2"><span class="fu">git</span> push origin master  </a>
<a class="sourceLine" id="cb3-3" data-line-number="3"><span class="fu">git</span> push -f origin master <span class="co"># 强制推送文件，缩写 -f（全写--force）</span></a></code></pre></div>
<p>clone</p>
<p><code>git clone git://github.com/JSLite/JSLite.js.git</code><br />
<code>git clone git://github.com/JSLite/JSLite.js.git mypro</code> #克隆到自定义文件夹<br />
<code>git clone [user@]example.com:path/to/repo.git/</code> #SSH协议还有另一种写法。</p>
<p>git clone支持多种协议，除了HTTP(s)以外，还支持SSH、Git、本地文件协议等，下面是一些例子。<code>git clone &lt;版本库的网址&gt; &lt;本地目录名&gt;</code></p>
<div class="sourceCode" id="cb4"><pre><code class="language-bash"><a class="sourceLine" id="cb4-1" data-line-number="1">$ <span class="fu">git</span> clone http[s]://example.com/path/to/repo.git/</a>
<a class="sourceLine" id="cb4-2" data-line-number="2">$ <span class="fu">git</span> clone ssh://example.com/path/to/repo.git/</a>
<a class="sourceLine" id="cb4-3" data-line-number="3">$ <span class="fu">git</span> clone git://example.com/path/to/repo.git/</a>
<a class="sourceLine" id="cb4-4" data-line-number="4">$ <span class="fu">git</span> clone /opt/git/project.git </a>
<a class="sourceLine" id="cb4-5" data-line-number="5">$ <span class="fu">git</span> clone file:///opt/git/project.git</a>
<a class="sourceLine" id="cb4-6" data-line-number="6">$ <span class="fu">git</span> clone ftp[s]://example.com/path/to/repo.git/</a>
<a class="sourceLine" id="cb4-7" data-line-number="7">$ <span class="fu">git</span> clone rsync://example.com/path/to/repo.git/</a></code></pre></div>
<h2 id="配置">配置</h2>
<p>首先是配置帐号信息 <code>ssh -T git@github.com</code> 测试。</p>
<h2 id="修改项目中的个人信息">修改项目中的个人信息</h2>
<div class="sourceCode" id="cb5"><pre><code class="language-bash"><a class="sourceLine" id="cb5-1" data-line-number="1"><span class="fu">git</span> help config <span class="co"># 获取帮助信息，查看修改个人信息的参数  </span></a>
<a class="sourceLine" id="cb5-2" data-line-number="2"><span class="fu">git</span> config --global user.name <span class="st">&quot;小弟调调&quot;</span>           # 修改全局名字</a>
<a class="sourceLine" id="cb5-3" data-line-number="3"><span class="fu">git</span> config --global user.email <span class="st">&quot;wowohoo@qq.com&quot;</span>  # 修改全局邮箱</a>
<a class="sourceLine" id="cb5-4" data-line-number="4"><span class="fu">git</span> config --list         # 查看配置的信息  </a></code></pre></div>
<h3 id="配置自动换行">配置自动换行</h3>
<p>自动转换坑太大，提交到git是自动将换行符转换为lf</p>
<div class="sourceCode" id="cb6"><pre><code class="language-bash"><a class="sourceLine" id="cb6-1" data-line-number="1"><span class="fu">git</span> config --global core.autocrlf input</a></code></pre></div>
<h2 id="常见使用场景">常见使用场景</h2>
<h3 id="创建ssh密钥">创建SSH密钥</h3>
<p>这个密钥用来跟 github 通信，在本地终端里生成然后上传到 github</p>
<div class="sourceCode" id="cb7"><pre><code class="language-bash"><a class="sourceLine" id="cb7-1" data-line-number="1"><span class="fu">ssh-keygen</span> -t rsa -C <span class="st">&#39;wowohoo@qq.com&#39;</span> <span class="co"># 生成密钥  </span></a>
<a class="sourceLine" id="cb7-2" data-line-number="2"><span class="fu">ssh-keygen</span> -t rsa -C <span class="st">&quot;wowohoo@qq.com&quot;</span> -f ~/.ssh/ww_rsa <span class="co"># 指定生成目录文件名字</span></a>
<a class="sourceLine" id="cb7-3" data-line-number="3"><span class="fu">ssh</span> -T git@github.com <span class="co"># 测试是否成功  </span></a></code></pre></div>
<h3 id="多账号ssh配置">多账号ssh配置</h3>
<p><strong>1.生成指定名字的密钥</strong></p>
<p><code>ssh-keygen -t rsa -C &quot;邮箱地址&quot; -f ~/.ssh/jslite_rsa</code><br />
会生成 <code>jslite_rsa</code> 和 <code>jslite_rsa.pub</code> 这两个文件</p>
<p><strong>2.密钥复制到托管平台上</strong></p>
<p><code>vim ~/.ssh/jslite_rsa.pub</code><br />
打开公钥文件 <code>jslite_rsa.pub</code> ，并把内容复制至代码托管平台上</p>
<p><strong>3.修改config文件</strong></p>
<p><code>vim ~/.ssh/config</code> #修改config文件，如果没有创建 <code>config</code></p>
<div class="sourceCode" id="cb8"><pre><code class="language-bash"><a class="sourceLine" id="cb8-1" data-line-number="1"><span class="ex">Host</span> jslite.github.com</a>
<a class="sourceLine" id="cb8-2" data-line-number="2">  <span class="ex">HostName</span> github.com</a>
<a class="sourceLine" id="cb8-3" data-line-number="3">  <span class="ex">User</span> git</a>
<a class="sourceLine" id="cb8-4" data-line-number="4">  <span class="ex">IdentityFile</span> ~/.ssh/jslite_rsa</a>
<a class="sourceLine" id="cb8-5" data-line-number="5"></a>
<a class="sourceLine" id="cb8-6" data-line-number="6"><span class="ex">Host</span> work.github.com</a>
<a class="sourceLine" id="cb8-7" data-line-number="7">  <span class="ex">HostName</span> github.com</a>
<a class="sourceLine" id="cb8-8" data-line-number="8">  <span class="co"># Port 服务器open-ssh端口（默认：22,默认时一般不写此行）</span></a>
<a class="sourceLine" id="cb8-9" data-line-number="9">  <span class="co"># PreferredAuthentications 配置登录时用什么权限认证 </span></a>
<a class="sourceLine" id="cb8-10" data-line-number="10">  <span class="co">#                          publickey|password publickey|keyboard-interactive等</span></a>
<a class="sourceLine" id="cb8-11" data-line-number="11">  <span class="ex">User</span> git</a>
<a class="sourceLine" id="cb8-12" data-line-number="12">  <span class="ex">IdentityFile</span> ~/.ssh/work_rsa</a></code></pre></div>
<ul>
<li><code>Host</code> 这里是个别名可以随便命名</li>
<li><code>HostName</code> 一般是网站如：<code>git@ss.github.com:username/repo.git</code> 填写 <code>github.com</code></li>
<li><code>User</code> 通常填写<code>git</code></li>
<li><code>IdentityFile</code> 使用的公钥文件地址</li>
</ul>
<p><strong>4.测试</strong></p>
<div class="sourceCode" id="cb9"><pre><code class="language-bash"><a class="sourceLine" id="cb9-1" data-line-number="1"><span class="fu">ssh</span> -T git@jslite.github.com  # <span class="kw">`</span><span class="ex">@</span><span class="kw">`</span>后面跟上定义的Host  </a>
<a class="sourceLine" id="cb9-2" data-line-number="2"><span class="fu">ssh</span> -T work.github.com        # 通过别名测试</a>
<a class="sourceLine" id="cb9-3" data-line-number="3"><span class="fu">ssh</span> -i ~/公钥文件地址 Host别名  # 如 ssh -i ~/.ssh/work_rsa work.github.com</a></code></pre></div>
<p><strong>5.使用</strong></p>
<div class="sourceCode" id="cb10"><pre><code class="language-bash"><a class="sourceLine" id="cb10-1" data-line-number="1"><span class="co"># 原来的写法</span></a>
<a class="sourceLine" id="cb10-2" data-line-number="2"><span class="fu">git</span> clone git@github.com:<span class="op">&lt;</span>jslite的用户名<span class="op">&gt;</span>/learngit.git</a>
<a class="sourceLine" id="cb10-3" data-line-number="3"><span class="co"># 现在的写法</span></a>
<a class="sourceLine" id="cb10-4" data-line-number="4"><span class="fu">git</span> clone git@jslite.github.com:<span class="op">&lt;</span>jslite的用户名<span class="op">&gt;</span>/learngit.git</a>
<a class="sourceLine" id="cb10-5" data-line-number="5"><span class="fu">git</span> clone git@work.github.com:<span class="op">&lt;</span>work的用户名<span class="op">&gt;</span>/learngit.git</a></code></pre></div>
<p><strong>5.注意</strong></p>
<p>如果你修改了id_rsa的名字，你需要将ssh key添加到SSH agent中，如：</p>
<div class="sourceCode" id="cb11"><pre><code class="language-bash"><a class="sourceLine" id="cb11-1" data-line-number="1"><span class="fu">ssh-add</span> ~/.ssh/jslite_rsa</a>
<a class="sourceLine" id="cb11-2" data-line-number="2"><span class="fu">ssh-add</span> -l  # 查看所有的key</a>
<a class="sourceLine" id="cb11-3" data-line-number="3"><span class="fu">ssh-add</span> -D  # 删除所有的key</a>
<a class="sourceLine" id="cb11-4" data-line-number="4"><span class="fu">ssh-add</span> -d  ~/.ssh/jslite_rsa <span class="co"># 删除指定的key</span></a></code></pre></div>
<h3 id="免密码登录远程服务器">免密码登录远程服务器</h3>
<div class="sourceCode" id="cb12"><pre><code class="language-bash"><a class="sourceLine" id="cb12-1" data-line-number="1">$ <span class="fu">ssh-keygen</span> -t rsa -P <span class="st">&#39;&#39;</span> -f ~/.ssh/aliyunserver.key</a>
<a class="sourceLine" id="cb12-2" data-line-number="2">$ <span class="ex">ssh-copy-id</span> -i ~/.ssh/aliyunserver.key.pub root@192.168.182.112 <span class="co"># 这里需要输入密码一次</span></a></code></pre></div>
<p>编辑 <code>~/.ssh/config</code></p>
<div class="sourceCode" id="cb13"><pre><code class="language-bash"><a class="sourceLine" id="cb13-1" data-line-number="1"><span class="ex">Host</span> aliyun1</a>
<a class="sourceLine" id="cb13-2" data-line-number="2">  <span class="ex">HostName</span> 192.168.182.112</a>
<a class="sourceLine" id="cb13-3" data-line-number="3">  <span class="ex">User</span> root</a>
<a class="sourceLine" id="cb13-4" data-line-number="4">  <span class="ex">PreferredAuthentications</span> publickey</a>
<a class="sourceLine" id="cb13-5" data-line-number="5">  <span class="ex">IdentityFile</span> ~/.ssh/aliyunserver.key</a></code></pre></div>
<p>上面配置完了，可以通过命令登录，不需要输入IP地址和密码 <code>ssh aliyun1</code></p>
<h3 id="https协议下提交代码免密码">https协议下提交代码免密码</h3>
<div class="sourceCode" id="cb14"><pre><code class="language-bash"><a class="sourceLine" id="cb14-1" data-line-number="1"><span class="fu">git</span> clone https://github.com/username/rep.git</a></code></pre></div>
<p>通过上面方式克隆可能需要密码，解决办法：进入当前克隆的项目 <code>vi rep/.git/config</code> 编辑 <code>config</code>, 按照下面方式修改，你就可以提交代码不用输入密码了。</p>
<div class="sourceCode" id="cb15"><pre class="sourceCode diff"><code class="sourceCode diff"><a class="sourceLine" id="cb15-1" data-line-number="1">[core]</a>
<a class="sourceLine" id="cb15-2" data-line-number="2">    repositoryformatversion = 0</a>
<a class="sourceLine" id="cb15-3" data-line-number="3">    filemode = true</a>
<a class="sourceLine" id="cb15-4" data-line-number="4">    bare = false</a>
<a class="sourceLine" id="cb15-5" data-line-number="5">    logallrefupdates = true</a>
<a class="sourceLine" id="cb15-6" data-line-number="6">    ignorecase = true</a>
<a class="sourceLine" id="cb15-7" data-line-number="7">    precomposeunicode = true</a>
<a class="sourceLine" id="cb15-8" data-line-number="8">[remote &quot;origin&quot;]</a>
<a class="sourceLine" id="cb15-9" data-line-number="9"><span class="st">-   url = https://github.com/username/rep.git</span></a>
<a class="sourceLine" id="cb15-10" data-line-number="10"><span class="va">+   url = https://用户名:密码@github.com/username/rep.git</span></a>
<a class="sourceLine" id="cb15-11" data-line-number="11">    fetch = +refs/heads/*:refs/remotes/origin/*</a>
<a class="sourceLine" id="cb15-12" data-line-number="12">[branch &quot;master&quot;]</a>
<a class="sourceLine" id="cb15-13" data-line-number="13">    remote = origin</a>
<a class="sourceLine" id="cb15-14" data-line-number="14">    merge = refs/heads/master</a></code></pre></div>
<h3 id="文件推向3个git库">文件推向3个git库</h3>
<p><strong>1. 增加3个远程库地址</strong></p>
<div class="sourceCode" id="cb16"><pre><code class="language-bash"><a class="sourceLine" id="cb16-1" data-line-number="1"><span class="fu">git</span> remote add origin https://github.com/JSLite/JSLite.git  </a>
<a class="sourceLine" id="cb16-2" data-line-number="2"><span class="fu">git</span> remote set-url --add origin https://gitlab.com/wang/JSLite.js.git  </a>
<a class="sourceLine" id="cb16-3" data-line-number="3"><span class="fu">git</span> remote set-url --add origin https://oschina.net/wang/JSLite.js.git  </a></code></pre></div>
<p><strong>2. 删除其中一个 set-url 地址</strong></p>
<div class="sourceCode" id="cb17"><pre><code class="language-bash"><a class="sourceLine" id="cb17-1" data-line-number="1"><span class="ex">usage</span>: git remote set-url [--push] <span class="op">&lt;</span>name<span class="op">&gt;</span> <span class="op">&lt;</span>newurl<span class="op">&gt;</span> [<span class="op">&lt;</span>oldurl<span class="op">&gt;</span>]</a>
<a class="sourceLine" id="cb17-2" data-line-number="2">   <span class="ex">or</span>: git remote set-url --add <span class="op">&lt;</span>name<span class="op">&gt;</span> <span class="op">&lt;</span>newurl<span class="op">&gt;</span></a>
<a class="sourceLine" id="cb17-3" data-line-number="3">   <span class="ex">or</span>: git remote set-url --delete <span class="op">&lt;</span>name<span class="op">&gt;</span> <span class="op">&lt;</span>url<span class="op">&gt;</span></a></code></pre></div>
<p><code>git remote set-url --delete origin https://oschina.net/wang/JSLite.js.git</code></p>
<p><strong>3.推送代码</strong></p>
<div class="sourceCode" id="cb18"><pre><code class="language-bash"><a class="sourceLine" id="cb18-1" data-line-number="1"><span class="fu">git</span> push origin master</a>
<a class="sourceLine" id="cb18-2" data-line-number="2"><span class="fu">git</span> push -f origin master  # 强制推送  </a></code></pre></div>
<p><strong>4.拉代码</strong></p>
<p>只能拉取 <code>origin</code> 里的一个url地址，这个fetch-url<br />
默认为你添加的到 <code>origin</code>的第一个地址</p>
<div class="sourceCode" id="cb19"><pre><code class="language-bash"><a class="sourceLine" id="cb19-1" data-line-number="1"><span class="fu">git</span> pull origin master   </a>
<a class="sourceLine" id="cb19-2" data-line-number="2"><span class="fu">git</span> pull --all <span class="co"># 获取远程所有内容包括tag  </span></a>
<a class="sourceLine" id="cb19-3" data-line-number="3"><span class="fu">git</span> pull origin next:master <span class="co"># 取回origin主机的next分支，与本地的master分支合并  </span></a>
<a class="sourceLine" id="cb19-4" data-line-number="4"><span class="fu">git</span> pull origin next <span class="co"># 远程分支是与当前分支合并  </span></a>
<a class="sourceLine" id="cb19-5" data-line-number="5"></a>
<a class="sourceLine" id="cb19-6" data-line-number="6"><span class="co"># 上面一条命令等同于下面两条命令   </span></a>
<a class="sourceLine" id="cb19-7" data-line-number="7"><span class="fu">git</span> fetch origin  </a>
<a class="sourceLine" id="cb19-8" data-line-number="8"><span class="fu">git</span> merge origin/next  </a></code></pre></div>
<p>如果远程主机删除了某个分支，默认情况下，git pull 不会在拉取远程分支的时候，删除对应的本地分支。这是为了防止，由于其他人操作了远程主机，导致git pull不知不觉删除了本地分支。<br />
但是，你可以改变这个行为，加上参数 -p 就会在本地删除远程已经删除的分支。</p>
<div class="sourceCode" id="cb20"><pre><code class="language-bash"><a class="sourceLine" id="cb20-1" data-line-number="1">$ <span class="fu">git</span> pull -p</a>
<a class="sourceLine" id="cb20-2" data-line-number="2"><span class="co"># 等同于下面的命令</span></a>
<a class="sourceLine" id="cb20-3" data-line-number="3">$ <span class="fu">git</span> fetch --prune origin </a>
<a class="sourceLine" id="cb20-4" data-line-number="4">$ <span class="fu">git</span> fetch -p</a></code></pre></div>
<p><strong>5.更改pull</strong></p>
<p>只需要更改config文件里，那三个url的顺序即可，fetch-url会直接对应排行第一的那个utl连接。</p>
<h3 id="修改远程仓库地址">修改远程仓库地址</h3>
<div class="sourceCode" id="cb21"><pre><code class="language-bash"><a class="sourceLine" id="cb21-1" data-line-number="1"><span class="fu">git</span> remote remove origin  # 删除该远程路径  </a>
<a class="sourceLine" id="cb21-2" data-line-number="2"><span class="fu">git</span> remote add origin git@jslite.github.com:JSLite/JSLite.git  # 添加远程路径 </a></code></pre></div>
<h3 id="撤销远程记录">撤销远程记录</h3>
<div class="sourceCode" id="cb22"><pre><code class="language-bash"><a class="sourceLine" id="cb22-1" data-line-number="1"><span class="fu">git</span> reset --hard HEAD~1 <span class="co"># 撤销一条记录   </span></a>
<a class="sourceLine" id="cb22-2" data-line-number="2"><span class="fu">git</span> push -f origin HEAD:master <span class="co"># 同步到远程仓库  </span></a></code></pre></div>
<h3 id="放弃本地的文件修改">放弃本地的文件修改</h3>
<div class="sourceCode" id="cb23"><pre><code class="language-bash"><a class="sourceLine" id="cb23-1" data-line-number="1"><span class="fu">git</span> reset --hard FETCH_HEAD <span class="co"># FETCH_HEAD表示上一次成功git pull之后形成的commit点。然后git pull</span></a></code></pre></div>
<p><code>git reset --hard FETCH_HEAD</code> 出现错误</p>
<div class="sourceCode" id="cb24"><pre><code class="language-bash"><a class="sourceLine" id="cb24-1" data-line-number="1"><span class="fu">git</span> pull</a>
<a class="sourceLine" id="cb24-2" data-line-number="2"><span class="ex">You</span> are not currently on a branch, so I cannot use any</a>
<a class="sourceLine" id="cb24-3" data-line-number="3"><span class="st">&#39;branch.&lt;branchname&gt;.merge&#39;</span> <span class="kw">in</span> <span class="ex">your</span> configuration file.</a>
<a class="sourceLine" id="cb24-4" data-line-number="4"><span class="ex">Please</span> specify which remote branch you want to use on the command</a>
<a class="sourceLine" id="cb24-5" data-line-number="5"><span class="ex">line</span> and try again (e.g. <span class="st">&#39;git pull &lt;repository&gt; &lt;refspec&gt;&#39;</span>)<span class="ex">.</span></a>
<a class="sourceLine" id="cb24-6" data-line-number="6"><span class="ex">See</span> git-pull(1) <span class="ex">FOR</span> details.</a></code></pre></div>
<p>解决方法：</p>
<div class="sourceCode" id="cb25"><pre><code class="language-bash"><a class="sourceLine" id="cb25-1" data-line-number="1"><span class="fu">git</span> checkout -b temp <span class="co"># 新建+切换到temp分支 </span></a>
<a class="sourceLine" id="cb25-2" data-line-number="2"><span class="fu">git</span> checkout master</a></code></pre></div>
<h3 id="最简单放弃本地修改内容">最简单放弃本地修改内容</h3>
<div class="sourceCode" id="cb26"><pre><code class="language-bash"><a class="sourceLine" id="cb26-1" data-line-number="1"><span class="co"># 如果有的修改以及加入暂存区的话</span></a>
<a class="sourceLine" id="cb26-2" data-line-number="2"><span class="fu">git</span> reset --hard </a>
<a class="sourceLine" id="cb26-3" data-line-number="3"><span class="co"># 还原所有修改，不会删除新增的文件</span></a>
<a class="sourceLine" id="cb26-4" data-line-number="4"><span class="fu">git</span> checkout . </a>
<a class="sourceLine" id="cb26-5" data-line-number="5"><span class="co"># 下面命令会删除新增的文件</span></a>
<a class="sourceLine" id="cb26-6" data-line-number="6"><span class="fu">git</span> clean -xdf</a></code></pre></div>
<p>通过存储暂存区stash，在删除暂存区的方法放弃本地修改。</p>
<div class="sourceCode" id="cb27"><pre><code class="language-bash"><a class="sourceLine" id="cb27-1" data-line-number="1"><span class="fu">git</span> stash <span class="kw">&amp;&amp;</span> <span class="fu">git</span> stash drop </a></code></pre></div>
<h3 id="回滚到某个commit提交">回滚到某个commit提交</h3>
<div class="sourceCode" id="cb28"><pre><code class="language-bash"><a class="sourceLine" id="cb28-1" data-line-number="1"><span class="fu">git</span> revert HEAD~1 <span class="co"># 撤销一条记录 会弹出 commit 编辑</span></a>
<a class="sourceLine" id="cb28-2" data-line-number="2"><span class="fu">git</span> push <span class="co"># 提交回滚</span></a></code></pre></div>
<h3 id="回退到某一个版本">回退到某一个版本</h3>
<div class="sourceCode" id="cb29"><pre><code class="language-bash"><a class="sourceLine" id="cb29-1" data-line-number="1"><span class="fu">git</span> reset --hard <span class="op">&lt;</span>hash<span class="op">&gt;</span></a>
<a class="sourceLine" id="cb29-2" data-line-number="2"><span class="co"># 例如 git reset --hard a3hd73r</span></a>
<a class="sourceLine" id="cb29-3" data-line-number="3"><span class="co"># --hard代表丢弃工作区的修改，让工作区与版本代码一模一样，与之对应，</span></a>
<a class="sourceLine" id="cb29-4" data-line-number="4"><span class="co"># --soft参数代表保留工作区的修改。</span></a></code></pre></div>
<h3 id="去掉某个commit">去掉某个commit</h3>
<div class="sourceCode" id="cb30"><pre><code class="language-bash"><a class="sourceLine" id="cb30-1" data-line-number="1"><span class="co"># 实质是新建了一个与原来完全相反的commit，抵消了原来commit的效果</span></a>
<a class="sourceLine" id="cb30-2" data-line-number="2"><span class="fu">git</span> revert <span class="op">&lt;</span>commit-hash<span class="op">&gt;</span> </a></code></pre></div>
<h3 id="新建一个空分支">新建一个空分支</h3>
<div class="sourceCode" id="cb31"><pre><code class="language-bash"><a class="sourceLine" id="cb31-1" data-line-number="1"><span class="co"># 这种方式新建的分支(gh-pages)是没有 commit 记录的</span></a>
<a class="sourceLine" id="cb31-2" data-line-number="2"><span class="fu">git</span> checkout --orphan gh-pages</a>
<a class="sourceLine" id="cb31-3" data-line-number="3"><span class="co"># 删除新建的gh-pages分支原本的内容，如果不删除，提交将作为当前分支的第一个commit</span></a>
<a class="sourceLine" id="cb31-4" data-line-number="4"><span class="fu">git</span> rm -rf .</a>
<a class="sourceLine" id="cb31-5" data-line-number="5"><span class="co"># 查看一下状态 有可能上面一条命令，没有删除还没有提交的的文件</span></a>
<a class="sourceLine" id="cb31-6" data-line-number="6"><span class="fu">git</span> state </a></code></pre></div>
<h3 id="合并多个commit">合并多个commit</h3>
<div class="sourceCode" id="cb32"><pre><code class="language-bash"><a class="sourceLine" id="cb32-1" data-line-number="1"><span class="co"># 这个命令，将最近4个commit合并为1个，HEAD代表当前版本。</span></a>
<a class="sourceLine" id="cb32-2" data-line-number="2"><span class="co"># 将进入VIM界面，你可以修改提交信息。</span></a>
<a class="sourceLine" id="cb32-3" data-line-number="3"><span class="fu">git</span> rebase -i HEAD~4 </a>
<a class="sourceLine" id="cb32-4" data-line-number="4"><span class="co"># 可以看到其中分为两个部分，上方未注释的部分是填写要执行的指令，</span></a>
<a class="sourceLine" id="cb32-5" data-line-number="5"><span class="co"># 而下方注释的部分则是指令的提示说明。指令部分中由前方的命令名称、commit hash 和 commit message 组成</span></a>
<a class="sourceLine" id="cb32-6" data-line-number="6"><span class="co"># 当前我们只要知道 pick 和 squash 这两个命令即可。</span></a>
<a class="sourceLine" id="cb32-7" data-line-number="7"><span class="co"># --&gt; pick 的意思是要会执行这个 commit</span></a>
<a class="sourceLine" id="cb32-8" data-line-number="8"><span class="co"># --&gt; squash 的意思是这个 commit 会被合并到前一个commit</span></a>
<a class="sourceLine" id="cb32-9" data-line-number="9"></a>
<a class="sourceLine" id="cb32-10" data-line-number="10"><span class="co"># 我们将 需要保留的 这个 commit 前方的命令改成 squash 或 s，然后输入:wq以保存并退出</span></a>
<a class="sourceLine" id="cb32-11" data-line-number="11"><span class="co"># 这是我们会看到 commit message 的编辑界面</span></a>
<a class="sourceLine" id="cb32-12" data-line-number="12"></a>
<a class="sourceLine" id="cb32-13" data-line-number="13"><span class="co"># 其中, 非注释部分就是两次的 commit message, 你要做的就是将这两个修改成新的 commit message。</span></a>
<a class="sourceLine" id="cb32-14" data-line-number="14"><span class="co"># </span></a>
<a class="sourceLine" id="cb32-15" data-line-number="15"><span class="co"># 输入wq保存并推出, 再次输入git log查看 commit 历史信息，你会发现这两个 commit 已经合并了。</span></a>
<a class="sourceLine" id="cb32-16" data-line-number="16"><span class="co"># 将修改强制推送到前端</span></a>
<a class="sourceLine" id="cb32-17" data-line-number="17"><span class="fu">git</span> push -f origin master</a></code></pre></div>
<h3 id="修改远程commit记录">修改远程Commit记录</h3>
<div class="sourceCode" id="cb33"><pre><code class="language-bash"><a class="sourceLine" id="cb33-1" data-line-number="1"><span class="fu">git</span> commit --amend</a>
<a class="sourceLine" id="cb33-2" data-line-number="2"><span class="co"># amend只能修改没有提交到线上的，最后一次commit记录</span></a>
<a class="sourceLine" id="cb33-3" data-line-number="3"><span class="fu">git</span> rebase -i HEAD~3</a>
<a class="sourceLine" id="cb33-4" data-line-number="4"><span class="co"># 表示要修改当前版本的倒数第三次状态</span></a>
<a class="sourceLine" id="cb33-5" data-line-number="5"><span class="co"># 将要更改的记录行首单词 pick 改为 edit</span></a>
<a class="sourceLine" id="cb33-6" data-line-number="6"><span class="ex">pick</span> 96dc3f9 doc: Update quick-start.md</a>
<a class="sourceLine" id="cb33-7" data-line-number="7"><span class="ex">pick</span> f1cce8a test(Transition):<span class="ex">Add</span> transition test (#47)</a>
<a class="sourceLine" id="cb33-8" data-line-number="8"><span class="ex">pick</span> 6293516 feat(Divider)<span class="bu">:</span> Add Divider component.</a>
<a class="sourceLine" id="cb33-9" data-line-number="9"><span class="co"># Rebase eeb03a4..6293516 onto eeb03a4 (3 commands)</span></a>
<a class="sourceLine" id="cb33-10" data-line-number="10"><span class="co">#</span></a>
<a class="sourceLine" id="cb33-11" data-line-number="11"><span class="co"># Commands:</span></a>
<a class="sourceLine" id="cb33-12" data-line-number="12"><span class="co"># p, pick = use commit</span></a>
<a class="sourceLine" id="cb33-13" data-line-number="13"><span class="co"># r, reword = use commit, but edit the commit message</span></a>
<a class="sourceLine" id="cb33-14" data-line-number="14"><span class="co"># e, edit = use commit, but stop for amending</span></a>
<a class="sourceLine" id="cb33-15" data-line-number="15"><span class="co"># s, squash = use commit, but meld into previous commit</span></a>
<a class="sourceLine" id="cb33-16" data-line-number="16"><span class="co"># f, fixup = like &quot;squash&quot;, but discard this commit&#39;s log message</span></a>
<a class="sourceLine" id="cb33-17" data-line-number="17"><span class="co"># x, exec = run command (the rest of the line) using shell</span></a>
<a class="sourceLine" id="cb33-18" data-line-number="18"><span class="co"># d, drop = remove commit</span></a></code></pre></div>
<p>保存并退出，会弹出下面提示</p>
<div class="sourceCode" id="cb34"><pre><code class="language-bash"><a class="sourceLine" id="cb34-1" data-line-number="1"><span class="co"># You can amend the commit now, with</span></a>
<a class="sourceLine" id="cb34-2" data-line-number="2"><span class="co"># </span></a>
<a class="sourceLine" id="cb34-3" data-line-number="3"><span class="co">#   git commit --amend</span></a>
<a class="sourceLine" id="cb34-4" data-line-number="4"><span class="co"># </span></a>
<a class="sourceLine" id="cb34-5" data-line-number="5"><span class="co"># Once you are satisfied with your changes, run</span></a>
<a class="sourceLine" id="cb34-6" data-line-number="6"><span class="co"># </span></a>
<a class="sourceLine" id="cb34-7" data-line-number="7"><span class="co">#   git rebase --continue</span></a>
<a class="sourceLine" id="cb34-8" data-line-number="8"></a>
<a class="sourceLine" id="cb34-9" data-line-number="9"><span class="co"># 通过这条命令进入编辑页面更改commit，保存退出</span></a>
<a class="sourceLine" id="cb34-10" data-line-number="10"><span class="fu">git</span> commit --amend</a>
<a class="sourceLine" id="cb34-11" data-line-number="11"><span class="co"># 保存退出确认修改，继续执行 rebase, </span></a>
<a class="sourceLine" id="cb34-12" data-line-number="12"><span class="fu">git</span> rebase --continue</a>
<a class="sourceLine" id="cb34-13" data-line-number="13"><span class="co"># 如果修改多条记录反复执行上面两条命令直到完成所有修改</span></a>
<a class="sourceLine" id="cb34-14" data-line-number="14"></a>
<a class="sourceLine" id="cb34-15" data-line-number="15"><span class="co"># 最后，确保别人没有提交进行push，最好不要加 -f 强制推送</span></a>
<a class="sourceLine" id="cb34-16" data-line-number="16"><span class="fu">git</span> push -f origin master</a></code></pre></div>
<h3 id="添加忽略文件">添加忽略文件</h3>
<pre><code class="language-bash">echo node_modules/ &gt;&gt; .gitignore</code></pre>
<h3 id="利用commit关闭一个issue">利用commit关闭一个issue</h3>
<p>这个功能在Github上可以玩儿，Gitlab上特别老的版本不能玩儿哦，那么如何跟随着commit关闭一个issue呢? 在confirm merge的时候可以使用一下命令来关闭相关issue:</p>
<p><code>fixes #xxx</code>、 <code>fixed #xxx</code>、 <code>fix #xxx</code>、 <code>closes #xxx</code>、 <code>close #xxx</code>、 <code>closed #xxx</code>、</p>
<h3 id="同步fork的上游仓库">同步fork的上游仓库</h3>
<p><a href="https://help.github.com/articles/syncing-a-fork/">Github教程同步fork教程</a>，<a href="http://www.miss77.net/549.html">在Github上同步一个分支(fork)</a></p>
<p><strong>设置添加多个远程仓库地址。</strong></p>
<p>在同步之前，需要创建一个远程点指向上游仓库(repo).如果你已经派生了一个原始仓库，可以按照如下方法做。</p>
<div class="sourceCode" id="cb36"><pre><code class="language-bash"><a class="sourceLine" id="cb36-1" data-line-number="1">$ <span class="fu">git</span> remote -v</a>
<a class="sourceLine" id="cb36-2" data-line-number="2"><span class="co"># List the current remotes （列出当前远程仓库）</span></a>
<a class="sourceLine" id="cb36-3" data-line-number="3"><span class="co"># origin  https://github.com/user/repo.git (fetch)</span></a>
<a class="sourceLine" id="cb36-4" data-line-number="4"><span class="co"># origin  https://github.com/user/repo.git (push)</span></a>
<a class="sourceLine" id="cb36-5" data-line-number="5">$ <span class="fu">git</span> remote add upstream https://github.com/otheruser/repo.git</a>
<a class="sourceLine" id="cb36-6" data-line-number="6"><span class="co"># Set a new remote (设置一个新的远程仓库)</span></a>
<a class="sourceLine" id="cb36-7" data-line-number="7">$ <span class="fu">git</span> remote -v</a>
<a class="sourceLine" id="cb36-8" data-line-number="8"><span class="co"># Verify new remote (验证新的原唱仓库)</span></a>
<a class="sourceLine" id="cb36-9" data-line-number="9"><span class="co"># origin    https://github.com/user/repo.git (fetch)</span></a>
<a class="sourceLine" id="cb36-10" data-line-number="10"><span class="co"># origin    https://github.com/user/repo.git (push)</span></a>
<a class="sourceLine" id="cb36-11" data-line-number="11"><span class="co"># upstream  https://github.com/otheruser/repo.git (fetch)</span></a>
<a class="sourceLine" id="cb36-12" data-line-number="12"><span class="co"># upstream  https://github.com/otheruser/repo.git (push)</span></a></code></pre></div>
<p><strong>同步更新仓库内容</strong></p>
<p>同步上游仓库到你的仓库需要执行两步：首先你需要从远程拉去，之后你需要合并你希望的分支到你的本地副本分支。从上游的存储库中提取分支以及各自的提交内容。 <code>master</code> 将被存储在本地分支机构 <code>upstream/master</code></p>
<div class="sourceCode" id="cb37"><pre><code class="language-bash"><a class="sourceLine" id="cb37-1" data-line-number="1"><span class="fu">git</span> fetch upstream</a>
<a class="sourceLine" id="cb37-2" data-line-number="2"><span class="co"># remote: Counting objects: 75, done.</span></a>
<a class="sourceLine" id="cb37-3" data-line-number="3"><span class="co"># remote: Compressing objects: 100% (53/53), done.</span></a>
<a class="sourceLine" id="cb37-4" data-line-number="4"><span class="co"># remote: Total 62 (delta 27), reused 44 (delta 9)</span></a>
<a class="sourceLine" id="cb37-5" data-line-number="5"><span class="co"># Unpacking objects: 100% (62/62), done.</span></a>
<a class="sourceLine" id="cb37-6" data-line-number="6"><span class="co"># From https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY</span></a>
<a class="sourceLine" id="cb37-7" data-line-number="7"><span class="co">#  * [new branch]      master     -&gt; upstream/master</span></a></code></pre></div>
<p>检查你的 fork’s 本地 <code>master</code> 分支</p>
<div class="sourceCode" id="cb38"><pre><code class="language-bash"><a class="sourceLine" id="cb38-1" data-line-number="1"><span class="fu">git</span> checkout master</a>
<a class="sourceLine" id="cb38-2" data-line-number="2"><span class="co"># Switched to branch &#39;master&#39;</span></a></code></pre></div>
<p>合并来自 <code>upstream/master</code> 的更改到本地 master 分支上。 这使你的前 fork’s <code>master</code> 分支与上游资源库同步，而不会丢失你本地修改。</p>
<div class="sourceCode" id="cb39"><pre><code class="language-bash"><a class="sourceLine" id="cb39-1" data-line-number="1"><span class="fu">git</span> merge upstream/master</a>
<a class="sourceLine" id="cb39-2" data-line-number="2"><span class="co"># Updating a422352..5fdff0f</span></a>
<a class="sourceLine" id="cb39-3" data-line-number="3"><span class="co"># Fast-forward</span></a>
<a class="sourceLine" id="cb39-4" data-line-number="4"><span class="co">#  README                    |    9 -------</span></a>
<a class="sourceLine" id="cb39-5" data-line-number="5"><span class="co">#  README.md                 |    7 ++++++</span></a>
<a class="sourceLine" id="cb39-6" data-line-number="6"><span class="co">#  2 files changed, 7 insertions(+), 9 deletions(-)</span></a>
<a class="sourceLine" id="cb39-7" data-line-number="7"><span class="co">#  delete mode 100644 README</span></a>
<a class="sourceLine" id="cb39-8" data-line-number="8"><span class="co">#  create mode 100644 README.md</span></a></code></pre></div>
<h3 id="批量修改历史commit中的名字和邮箱">批量修改历史commit中的名字和邮箱</h3>
<p><strong>1.克隆仓库</strong></p>
<p>注意参数，这个不是普通的clone，clone下来的仓库并不能参与开发</p>
<div class="sourceCode" id="cb40"><pre><code class="language-bash"><a class="sourceLine" id="cb40-1" data-line-number="1"><span class="fu">git</span> clone --bare https://github.com/user/repo.git</a>
<a class="sourceLine" id="cb40-2" data-line-number="2"><span class="bu">cd</span> repo.git</a></code></pre></div>
<p><strong>2.命令行中运行代码</strong></p>
<p>OLD_EMAIL原来的邮箱<br />
CORRECT_NAME更正的名字<br />
CORRECT_EMAIL更正的邮箱</p>
<p>将下面代码复制放到命令行中执行</p>
<div class="sourceCode" id="cb41"><pre><code class="language-bash"><a class="sourceLine" id="cb41-1" data-line-number="1"><span class="fu">git</span> filter-branch -f --env-filter <span class="st">&#39;</span></a>
<a class="sourceLine" id="cb41-2" data-line-number="2"><span class="st">OLD_EMAIL=&quot;wowohoo@qq.com&quot;</span></a>
<a class="sourceLine" id="cb41-3" data-line-number="3"><span class="st">CORRECT_NAME=&quot;小弟调调&quot;</span></a>
<a class="sourceLine" id="cb41-4" data-line-number="4"><span class="st">CORRECT_EMAIL=&quot;更正的邮箱@qq.com&quot;</span></a>
<a class="sourceLine" id="cb41-5" data-line-number="5"><span class="st">if [ &quot;$GIT_COMMITTER_EMAIL&quot; = &quot;$OLD_EMAIL&quot; ]</span></a>
<a class="sourceLine" id="cb41-6" data-line-number="6"><span class="st">then</span></a>
<a class="sourceLine" id="cb41-7" data-line-number="7"><span class="st">    export GIT_COMMITTER_NAME=&quot;$CORRECT_NAME&quot;</span></a>
<a class="sourceLine" id="cb41-8" data-line-number="8"><span class="st">    export GIT_COMMITTER_EMAIL=&quot;$CORRECT_EMAIL&quot;</span></a>
<a class="sourceLine" id="cb41-9" data-line-number="9"><span class="st">fi</span></a>
<a class="sourceLine" id="cb41-10" data-line-number="10"><span class="st">if [ &quot;$GIT_AUTHOR_EMAIL&quot; = &quot;$OLD_EMAIL&quot; ]</span></a>
<a class="sourceLine" id="cb41-11" data-line-number="11"><span class="st">then</span></a>
<a class="sourceLine" id="cb41-12" data-line-number="12"><span class="st">    export GIT_AUTHOR_NAME=&quot;$CORRECT_NAME&quot;</span></a>
<a class="sourceLine" id="cb41-13" data-line-number="13"><span class="st">    export GIT_AUTHOR_EMAIL=&quot;$CORRECT_EMAIL&quot;</span></a>
<a class="sourceLine" id="cb41-14" data-line-number="14"><span class="st">fi</span></a>
<a class="sourceLine" id="cb41-15" data-line-number="15"><span class="st">&#39;</span> --tag-name-filter cat -- --branches --tags</a></code></pre></div>
<p>执行过程</p>
<div class="sourceCode" id="cb42"><pre><code class="language-bash"><a class="sourceLine" id="cb42-1" data-line-number="1"><span class="ex">Rewrite</span> 160d4df2689ff6df3820563bfd13b5f1fb9ba832 (479/508) <span class="kw">(</span><span class="ex">16</span> seconds passed, remaining 0 predicted<span class="kw">)</span></a>
<a class="sourceLine" id="cb42-2" data-line-number="2"><span class="ex">Ref</span> <span class="st">&#39;refs/heads/dev&#39;</span> was rewritten</a>
<a class="sourceLine" id="cb42-3" data-line-number="3"><span class="ex">Ref</span> <span class="st">&#39;refs/heads/master&#39;</span> was rewritten</a></code></pre></div>
<p><strong>3.同步到远程仓库</strong></p>
<p>同步到push远程git仓库</p>
<div class="sourceCode" id="cb43"><pre><code class="language-bash"><a class="sourceLine" id="cb43-1" data-line-number="1"><span class="fu">git</span> push --force --tags origin <span class="st">&#39;refs/heads/*&#39;</span></a></code></pre></div>
<p>我还遇到了如下面错误，lab默认给master分支加了保护，不允许强制覆盖。<code>Project(项目)</code>-&gt;<code>Setting</code>-&gt;<code>Repository</code> 菜单下面的<code>Protected branches</code>把master的保护去掉就可以了。修改完之后，建议把master的保护再加回来，毕竟强推不是件好事。</p>
<div class="sourceCode" id="cb44"><pre><code class="language-bash"><a class="sourceLine" id="cb44-1" data-line-number="1"><span class="ex">remote</span>: GitLab: You are not allowed to force push code to a protected branch on this project.</a></code></pre></div>
<p>当上面的push 不上去的时候，先 <code>git pull</code> 确保最新代码</p>
<div class="sourceCode" id="cb45"><pre><code class="language-bash"><a class="sourceLine" id="cb45-1" data-line-number="1"><span class="fu">git</span> pull  --allow-unrelated-histories</a>
<a class="sourceLine" id="cb45-2" data-line-number="2"><span class="co"># 或者指定分枝</span></a>
<a class="sourceLine" id="cb45-3" data-line-number="3"><span class="fu">git</span> pull origin master --allow-unrelated-histories</a></code></pre></div>
<h3 id="查看某个文件历史">查看某个文件历史</h3>
<div class="sourceCode" id="cb46"><pre><code class="language-bash"><a class="sourceLine" id="cb46-1" data-line-number="1"><span class="fu">git</span> log --pretty=oneline 文件名  # 列出文件的所有改动历史  </a>
<a class="sourceLine" id="cb46-2" data-line-number="2"><span class="fu">git</span> show c178bf49   # 某次的改动的修改记录  </a>
<a class="sourceLine" id="cb46-3" data-line-number="3"><span class="fu">git</span> log -p c178bf49 <span class="co"># 某次的改动的修改记录  </span></a>
<a class="sourceLine" id="cb46-4" data-line-number="4"><span class="fu">git</span> blame 文件名     # 显示文件的每一行是在那个版本最后修改。  </a>
<a class="sourceLine" id="cb46-5" data-line-number="5"><span class="fu">git</span> whatchanged 文件名  # 显示某个文件的每个版本提交信息：提交日期，提交人员，版本号，提交备注（没有修改细节）  </a></code></pre></div>
<h3 id="打造自己的git命令">打造自己的git命令</h3>
<div class="sourceCode" id="cb47"><pre class="sourceCode sh"><code class="sourceCode bash"><a class="sourceLine" id="cb47-1" data-line-number="1"><span class="fu">git</span> config --global alias.st status</a>
<a class="sourceLine" id="cb47-2" data-line-number="2"><span class="fu">git</span> config --global alias.br branch</a>
<a class="sourceLine" id="cb47-3" data-line-number="3"><span class="fu">git</span> config --global alias.co checkout</a>
<a class="sourceLine" id="cb47-4" data-line-number="4"><span class="fu">git</span> config --global alias.ci commit</a></code></pre></div>
<p>配置好后再输入git命令的时候就不用再输入一大段了，例如我们要查看状态，只需：</p>
<div class="sourceCode" id="cb48"><pre class="sourceCode sh"><code class="sourceCode bash"><a class="sourceLine" id="cb48-1" data-line-number="1"><span class="fu">git</span> st</a></code></pre></div>
<h3 id="中文乱码的解决方案">中文乱码的解决方案</h3>
<div class="sourceCode" id="cb49"><pre><code class="language-bash"><a class="sourceLine" id="cb49-1" data-line-number="1"><span class="fu">git</span> config --global core.quotepath false</a></code></pre></div>
<h2 id="新建仓库">新建仓库</h2>
<h3 id="init">init</h3>
<p><code>git init</code> #初始化</p>
<h3 id="status">status</h3>
<p><code>git status</code> #获取状态</p>
<h3 id="add">add</h3>
<p><code>git add file</code> # .或*代表全部添加<br />
<code>git rm --cached &lt;added_file_to_undo&gt;</code> # 在commit之前撤销git add操作<br />
<code>git reset head</code> # 好像比上面<code>git rm --cached</code>更方便</p>
<h3 id="commit">commit</h3>
<p><code>git commit -m &quot;message&quot;</code> #此处注意乱码</p>
<h3 id="remote">remote</h3>
<p><code>git remote add origin git@github.com:JSLite/test.git</code> #添加源</p>
<h3 id="push">push</h3>
<div class="sourceCode" id="cb50"><pre><code class="language-bash"><a class="sourceLine" id="cb50-1" data-line-number="1"><span class="fu">git</span> push -u origin master <span class="co"># push同事设置默认跟踪分支  </span></a>
<a class="sourceLine" id="cb50-2" data-line-number="2"><span class="fu">git</span> push origin master  </a>
<a class="sourceLine" id="cb50-3" data-line-number="3"><span class="fu">git</span> push -f origin master <span class="co"># 强制推送文件，缩写 -f（全写--force）</span></a></code></pre></div>
<h2 id="clone">clone</h2>
<p><code>git clone git://github.com/JSLite/JSLite.js.git</code><br />
<code>git clone git://github.com/JSLite/JSLite.js.git mypro</code> #克隆到自定义文件夹<br />
<code>git clone [user@]example.com:path/to/repo.git/</code> #SSH协议还有另一种写法。</p>
<p>git clone支持多种协议，除了HTTP(s)以外，还支持SSH、Git、本地文件协议等，下面是一些例子。<code>git clone &lt;版本库的网址&gt; &lt;本地目录名&gt;</code></p>
<div class="sourceCode" id="cb51"><pre><code class="language-bash"><a class="sourceLine" id="cb51-1" data-line-number="1">$ <span class="fu">git</span> clone http[s]://example.com/path/to/repo.git/</a>
<a class="sourceLine" id="cb51-2" data-line-number="2">$ <span class="fu">git</span> clone ssh://example.com/path/to/repo.git/</a>
<a class="sourceLine" id="cb51-3" data-line-number="3">$ <span class="fu">git</span> clone git://example.com/path/to/repo.git/</a>
<a class="sourceLine" id="cb51-4" data-line-number="4">$ <span class="fu">git</span> clone /opt/git/project.git </a>
<a class="sourceLine" id="cb51-5" data-line-number="5">$ <span class="fu">git</span> clone file:///opt/git/project.git</a>
<a class="sourceLine" id="cb51-6" data-line-number="6">$ <span class="fu">git</span> clone ftp[s]://example.com/path/to/repo.git/</a>
<a class="sourceLine" id="cb51-7" data-line-number="7">$ <span class="fu">git</span> clone rsync://example.com/path/to/repo.git/</a></code></pre></div>
<h2 id="本地">本地</h2>
<h3 id="help">help</h3>
<div class="sourceCode" id="cb52"><pre><code class="language-bash"><a class="sourceLine" id="cb52-1" data-line-number="1"><span class="fu">git</span> help config <span class="co"># 获取帮助信息  </span></a></code></pre></div>
<h3 id="add-1">add</h3>
<div class="sourceCode" id="cb53"><pre><code class="language-bash"><a class="sourceLine" id="cb53-1" data-line-number="1"><span class="fu">git</span> add *   # 跟踪新文件   </a>
<a class="sourceLine" id="cb53-2" data-line-number="2"><span class="fu">git</span> add -u [path]   # 添加[指定路径下]已跟踪文件   </a></code></pre></div>
<h3 id="rm">rm</h3>
<div class="sourceCode" id="cb54"><pre><code class="language-bash"><a class="sourceLine" id="cb54-1" data-line-number="1"><span class="fu">rm</span> *<span class="kw">&amp;</span><span class="fu">git</span> rm *          # 移除文件  </a>
<a class="sourceLine" id="cb54-2" data-line-number="2"><span class="fu">git</span> rm -f *            # 移除文件  </a>
<a class="sourceLine" id="cb54-3" data-line-number="3"><span class="fu">git</span> rm --cached *      # 取消跟踪  </a>
<a class="sourceLine" id="cb54-4" data-line-number="4"><span class="fu">git</span> mv file_from file_to  # 重命名跟踪文件  </a>
<a class="sourceLine" id="cb54-5" data-line-number="5"><span class="fu">git</span> log   # 查看提交记录  </a></code></pre></div>
<h3 id="commit-1">commit</h3>
<div class="sourceCode" id="cb55"><pre><code class="language-bash"><a class="sourceLine" id="cb55-1" data-line-number="1"><span class="fu">git</span> commit <span class="co">#提交更新   </span></a>
<a class="sourceLine" id="cb55-2" data-line-number="2"><span class="fu">git</span> commit -m <span class="st">&#39;message&#39;</span> <span class="co">#提交说明   </span></a>
<a class="sourceLine" id="cb55-3" data-line-number="3"><span class="fu">git</span> commit -a <span class="co">#跳过使用暂存区域，把所有已经跟踪过的文件暂存起来一并提交   </span></a>
<a class="sourceLine" id="cb55-4" data-line-number="4"><span class="fu">git</span> commit --amend <span class="co">#修改最后一次提交   </span></a>
<a class="sourceLine" id="cb55-5" data-line-number="5"><span class="fu">git</span> commit log <span class="co">#查看所有提交，包括没有push的commit    </span></a>
<a class="sourceLine" id="cb55-6" data-line-number="6"><span class="fu">git</span> commit -m <span class="st">&quot;#133&quot;</span> <span class="co">#关联issue 任意位置带上# 符号加上issue号码  </span></a>
<a class="sourceLine" id="cb55-7" data-line-number="7"><span class="fu">git</span> commit -m <span class="st">&quot;fix #133&quot;</span> commit关闭issue  </a>
<a class="sourceLine" id="cb55-8" data-line-number="8"><span class="fu">git</span> commit -m <span class="st">&#39;概要描述&#39;$&#39;</span><span class="dt">\n\n</span><span class="st">&#39;&#39;1.详细描述&#39;$&#39;</span><span class="dt">\n</span><span class="st">&#39;&#39;2.详细描述&#39;</span> <span class="co">#提交简要描述和详细描述  </span></a></code></pre></div>
<h3 id="reset">reset</h3>
<div class="sourceCode" id="cb56"><pre><code class="language-bash"><a class="sourceLine" id="cb56-1" data-line-number="1"><span class="fu">git</span> reset HEAD *  # 取消已经暂存的文件   </a>
<a class="sourceLine" id="cb56-2" data-line-number="2"><span class="fu">git</span> reset --mixed HEAD * <span class="co"># 同上   </span></a>
<a class="sourceLine" id="cb56-3" data-line-number="3"><span class="fu">git</span> reset --soft HEAD *  # 重置到指定状态，不会修改索引区和工作树   </a>
<a class="sourceLine" id="cb56-4" data-line-number="4"><span class="fu">git</span> reset --hard HEAD *  # 重置到指定状态，会修改索引区和工作树   </a>
<a class="sourceLine" id="cb56-5" data-line-number="5"><span class="fu">git</span> reset -- files *     # 重置index区文件   </a></code></pre></div>
<h3 id="revert">revert</h3>
<div class="sourceCode" id="cb57"><pre><code class="language-bash"><a class="sourceLine" id="cb57-1" data-line-number="1"><span class="fu">git</span> revert HEAD   # 撤销前一次操作   </a>
<a class="sourceLine" id="cb57-2" data-line-number="2"><span class="fu">git</span> revert HEAD~  # 撤销前前一次操作   </a>
<a class="sourceLine" id="cb57-3" data-line-number="3"><span class="fu">git</span> revert commit <span class="co"># 撤销指定操作   </span></a></code></pre></div>
<h3 id="checkout">checkout</h3>
<div class="sourceCode" id="cb58"><pre><code class="language-bash"><a class="sourceLine" id="cb58-1" data-line-number="1"><span class="fu">git</span> checkout -- file  # 取消对文件的修改（从暂存区——覆盖worktree file）  </a>
<a class="sourceLine" id="cb58-2" data-line-number="2"><span class="fu">git</span> checkout branch<span class="kw">|</span><span class="ex">tag</span><span class="kw">|</span><span class="ex">commit</span> -- file_name  # 从仓库取出file覆盖当前分支   </a>
<a class="sourceLine" id="cb58-3" data-line-number="3"><span class="fu">git</span> checkout HEAD~1 [文件]  # 将会更新 working directory 去匹配某次 commit   </a>
<a class="sourceLine" id="cb58-4" data-line-number="4"><span class="fu">git</span> checkout -- .          # 从暂存区取出文件覆盖工作区   </a>
<a class="sourceLine" id="cb58-5" data-line-number="5"><span class="fu">git</span> checkout -b gh-pages  0c304c9  # 这个表示 从当前分支 commit 哈希值为 0c304c9 的节点，分一个新的分支gh-pages出来，并切换到 gh-pages   </a></code></pre></div>
<h3 id="diff">diff</h3>
<div class="sourceCode" id="cb59"><pre><code class="language-bash"><a class="sourceLine" id="cb59-1" data-line-number="1"><span class="fu">git</span> diff file     # 查看指定文件的差异   </a>
<a class="sourceLine" id="cb59-2" data-line-number="2"><span class="fu">git</span> diff --stat   # 查看简单的diff结果   </a>
<a class="sourceLine" id="cb59-3" data-line-number="3"><span class="fu">git</span> diff  # 比较Worktree和Index之间的差异   </a>
<a class="sourceLine" id="cb59-4" data-line-number="4"><span class="fu">git</span> diff --cached   # 比较Index和HEAD之间的差异   </a>
<a class="sourceLine" id="cb59-5" data-line-number="5"><span class="fu">git</span> diff HEAD       # 比较Worktree和HEAD之间的差异   </a>
<a class="sourceLine" id="cb59-6" data-line-number="6"><span class="fu">git</span> diff branch     # 比较Worktree和branch之间的差异   </a>
<a class="sourceLine" id="cb59-7" data-line-number="7"><span class="fu">git</span> diff branch1 branch2  # 比较两次分支之间的差异   </a>
<a class="sourceLine" id="cb59-8" data-line-number="8"><span class="fu">git</span> diff commit commit    # 比较两次提交之间的差异   </a>
<a class="sourceLine" id="cb59-9" data-line-number="9"><span class="fu">git</span> diff master..test   # 上面这条命令只显示两个分支间的差异  </a>
<a class="sourceLine" id="cb59-10" data-line-number="10"><span class="fu">git</span> diff master...test    # 你想找出‘master’,‘test’的共有 父分支和<span class="st">&#39;test&#39;</span>分支之间的差异，你用3个‘.<span class="st">&#39;来取代前面的两个&#39;</span>.<span class="st">&#39;  </span></a></code></pre></div>
<h3 id="stash">stash</h3>
<div class="sourceCode" id="cb60"><pre><code class="language-bash"><a class="sourceLine" id="cb60-1" data-line-number="1"><span class="fu">git</span> stash <span class="co"># 将工作区现场（已跟踪文件）储藏起来，等以后恢复后继续工作。   </span></a>
<a class="sourceLine" id="cb60-2" data-line-number="2"><span class="fu">git</span> stash list  # 查看保存的工作现场   </a>
<a class="sourceLine" id="cb60-3" data-line-number="3"><span class="fu">git</span> stash apply <span class="co"># 恢复工作现场   </span></a>
<a class="sourceLine" id="cb60-4" data-line-number="4"><span class="fu">git</span> stash drop  # 删除stash内容   </a>
<a class="sourceLine" id="cb60-5" data-line-number="5"><span class="fu">git</span> stash pop   # 恢复的同时直接删除stash内容   </a>
<a class="sourceLine" id="cb60-6" data-line-number="6"><span class="fu">git</span> stash apply stash@<span class="dt">{0}</span> <span class="co"># 恢复指定的工作现场，当你保存了不只一份工作现场时。   </span></a></code></pre></div>
<h3 id="merge">merge</h3>
<div class="sourceCode" id="cb61"><pre><code class="language-bash"><a class="sourceLine" id="cb61-1" data-line-number="1"><span class="fu">git</span> merge --squash test <span class="co"># 合并压缩，将test上的commit压缩为一条   </span></a></code></pre></div>
<h3 id="cherry-pick">cherry-pick</h3>
<div class="sourceCode" id="cb62"><pre><code class="language-bash"><a class="sourceLine" id="cb62-1" data-line-number="1"><span class="fu">git</span> cherry-pick commit    # 拣选合并，将commit合并到当前分支   </a>
<a class="sourceLine" id="cb62-2" data-line-number="2"><span class="fu">git</span> cherry-pick -n commit <span class="co"># 拣选多个提交，合并完后可以继续拣选下一个提交   </span></a></code></pre></div>
<h3 id="rebase">rebase</h3>
<div class="sourceCode" id="cb63"><pre><code class="language-bash"><a class="sourceLine" id="cb63-1" data-line-number="1"><span class="fu">git</span> rebase master   # 将master分之上超前的提交，变基到当前分支  </a>
<a class="sourceLine" id="cb63-2" data-line-number="2"><span class="fu">git</span> rebase --onto master 169a6  # 限制回滚范围，rebase当前分支从169a6以后的提交  </a>
<a class="sourceLine" id="cb63-3" data-line-number="3"><span class="fu">git</span> rebase --interactive <span class="co"># 交互模式，修改commit   </span></a>
<a class="sourceLine" id="cb63-4" data-line-number="4"><span class="fu">git</span> rebase --continue    # 处理完冲突继续合并   </a>
<a class="sourceLine" id="cb63-5" data-line-number="5"><span class="fu">git</span> rebase --skip        # 跳过   </a>
<a class="sourceLine" id="cb63-6" data-line-number="6"><span class="fu">git</span> rebase --abort       # 取消合并    </a></code></pre></div>
<h2 id="分支branch">分支branch</h2>
<h3 id="删除">删除</h3>
<div class="sourceCode" id="cb64"><pre><code class="language-bash"><a class="sourceLine" id="cb64-1" data-line-number="1"><span class="fu">git</span> push origin :branchName  # 删除远程分支  </a>
<a class="sourceLine" id="cb64-2" data-line-number="2"><span class="fu">git</span> push origin --delete new <span class="co"># 删除远程分支new   </span></a>
<a class="sourceLine" id="cb64-3" data-line-number="3"><span class="fu">git</span> branch -d branchName     # 删除本地分支，强制删除用-D  </a>
<a class="sourceLine" id="cb64-4" data-line-number="4"><span class="fu">git</span> branch -d test      # 删除本地test分支   </a>
<a class="sourceLine" id="cb64-5" data-line-number="5"><span class="fu">git</span> branch -D test      # 强制删除本地test分支   </a>
<a class="sourceLine" id="cb64-6" data-line-number="6"><span class="fu">git</span> remote prune origin <span class="co"># 远程删除了，本地还能看到远程存在，这条命令删除远程不存在的分支</span></a></code></pre></div>
<h3 id="提交">提交</h3>
<div class="sourceCode" id="cb65"><pre><code class="language-bash"><a class="sourceLine" id="cb65-1" data-line-number="1"><span class="fu">git</span> push -u origin branchName <span class="co"># 提交分支到远程origin主机中  </span></a></code></pre></div>
<h3 id="拉取">拉取</h3>
<p><code>git fetch -p</code> #拉取远程分支时，自动清理 远程分支已删除，本地还存在的对应同名分支。</p>
<h3 id="分支合并">分支合并</h3>
<div class="sourceCode" id="cb66"><pre><code class="language-bash"><a class="sourceLine" id="cb66-1" data-line-number="1"><span class="fu">git</span> merge branchName      # 合并分支 - 将分支branchName和当前所在分支合并   </a>
<a class="sourceLine" id="cb66-2" data-line-number="2"><span class="fu">git</span> merge origin/master   # 在本地分支上合并远程分支。   </a>
<a class="sourceLine" id="cb66-3" data-line-number="3"><span class="fu">git</span> rebase origin/master  # 在本地分支上合并远程分支。   </a>
<a class="sourceLine" id="cb66-4" data-line-number="4"><span class="fu">git</span> merge test            # 将test分支合并到当前分支   </a></code></pre></div>
<h3 id="重命名">重命名</h3>
<p><code>git branch -m old new</code> #重命名分支</p>
<h3 id="查看">查看</h3>
<div class="sourceCode" id="cb67"><pre><code class="language-bash"><a class="sourceLine" id="cb67-1" data-line-number="1"><span class="fu">git</span> branch      # 列出本地分支   </a>
<a class="sourceLine" id="cb67-2" data-line-number="2"><span class="fu">git</span> branch -r   # 列出远端分支   </a>
<a class="sourceLine" id="cb67-3" data-line-number="3"><span class="fu">git</span> branch -a   # 列出所有分支   </a>
<a class="sourceLine" id="cb67-4" data-line-number="4"><span class="fu">git</span> branch -v   # 查看各个分支最后一个提交对象的信息   </a>
<a class="sourceLine" id="cb67-5" data-line-number="5"><span class="fu">git</span> branch --merge      # 查看已经合并到当前分支的分支   </a>
<a class="sourceLine" id="cb67-6" data-line-number="6"><span class="fu">git</span> branch --no-merge   # 查看为合并到当前分支的分支   </a>
<a class="sourceLine" id="cb67-7" data-line-number="7"><span class="fu">git</span> remote show origin  # 可以查看remote地址，远程分支</a></code></pre></div>
<h3 id="新建">新建</h3>
<div class="sourceCode" id="cb68"><pre><code class="language-bash"><a class="sourceLine" id="cb68-1" data-line-number="1"><span class="fu">git</span> branch test <span class="co"># 新建test分支  </span></a>
<a class="sourceLine" id="cb68-2" data-line-number="2"><span class="fu">git</span> branch newBrach 3defc69 <span class="co"># 指定哈希3defc69，新建分支名字为newBrach</span></a>
<a class="sourceLine" id="cb68-3" data-line-number="3"><span class="fu">git</span> checkout -b newBrach origin/master <span class="co"># 取回远程主机的更新以后，在它的基础上创建一个新的分支  </span></a>
<a class="sourceLine" id="cb68-4" data-line-number="4"><span class="fu">git</span> checkout -b newBrach 3defc69 <span class="co"># 以哈希值3defc69，新建 newBrach 分支，并切换到该分支</span></a></code></pre></div>
<h3 id="连接">连接</h3>
<div class="sourceCode" id="cb69"><pre><code class="language-bash"><a class="sourceLine" id="cb69-1" data-line-number="1"><span class="fu">git</span> branch --set-upstream dev origin/dev     # 将本地dev分支与远程dev分支之间建立链接  </a>
<a class="sourceLine" id="cb69-2" data-line-number="2"><span class="fu">git</span> branch --set-upstream master origin/next <span class="co"># 手动建立追踪关系  </span></a></code></pre></div>
<h3 id="分支切换">分支切换</h3>
<div class="sourceCode" id="cb70"><pre><code class="language-bash"><a class="sourceLine" id="cb70-1" data-line-number="1"><span class="fu">git</span> checkout test     # 切换到test分支   </a>
<a class="sourceLine" id="cb70-2" data-line-number="2"><span class="fu">git</span> checkout -b test  # 新建+切换到test分支   </a>
<a class="sourceLine" id="cb70-3" data-line-number="3"><span class="fu">git</span> checkout -b test dev <span class="co"># 基于dev新建test分支，并切换   </span></a></code></pre></div>
<h2 id="远端">远端</h2>
<div class="sourceCode" id="cb71"><pre><code class="language-bash"><a class="sourceLine" id="cb71-1" data-line-number="1"><span class="fu">git</span> fetch <span class="op">&lt;</span>远程主机名<span class="op">&gt;</span> <span class="op">&lt;</span>分支名<span class="op">&gt;</span>   # fetch取回所有分支（branch）的更新  </a>
<a class="sourceLine" id="cb71-2" data-line-number="2"><span class="fu">git</span> fetch origin remotebranch[:localbranch]   #  从远端拉去分支[到本地指定分支]   </a>
<a class="sourceLine" id="cb71-3" data-line-number="3"><span class="fu">git</span> merge origin/branch   # 合并远端上指定分支   </a>
<a class="sourceLine" id="cb71-4" data-line-number="4"><span class="fu">git</span> pull origin remotebranch:localbranch  #  拉去远端分支到本地分支   </a>
<a class="sourceLine" id="cb71-5" data-line-number="5"><span class="fu">git</span> push origin branch    # 将当前分支，推送到远端上指定分支   </a>
<a class="sourceLine" id="cb71-6" data-line-number="6"><span class="fu">git</span> push origin localbranch:remotebranch  # 推送本地指定分支，到远端上指定分支   </a>
<a class="sourceLine" id="cb71-7" data-line-number="7"><span class="fu">git</span> push origin :remotebranch   # 删除远端指定分支   </a>
<a class="sourceLine" id="cb71-8" data-line-number="8"><span class="fu">git</span> checkout -b [--track] test origin/dev <span class="co"># 基于远端dev分支，新建本地test分支[同时设置跟踪]  </span></a></code></pre></div>
<h2 id="submodule">submodule</h2>
<p>克隆项目同时克隆submodule</p>
<div class="sourceCode" id="cb72"><pre><code class="language-bash"><a class="sourceLine" id="cb72-1" data-line-number="1"><span class="fu">git</span> clone https://github.com/jaywcjlove/handbook.git --depth=1 --recurse-submodules</a></code></pre></div>
<p>克隆项目，之后再手动克隆 submodule 子项目</p>
<div class="sourceCode" id="cb73"><pre><code class="language-bash"><a class="sourceLine" id="cb73-1" data-line-number="1"><span class="fu">git</span> submodule add --force <span class="st">&#39;仓库地址&#39;</span> <span class="st">&#39;路径&#39;</span></a>
<a class="sourceLine" id="cb73-2" data-line-number="2"><span class="co"># 其中，仓库地址是指子模块仓库地址，路径指将子模块放置在当前工程下的路径。</span></a>
<a class="sourceLine" id="cb73-3" data-line-number="3"><span class="co"># 注意：路径不能以 / 结尾（会造成修改不生效）、不能是现有工程已有的目录（不能順利 Clone）</span></a>
<a class="sourceLine" id="cb73-4" data-line-number="4"><span class="fu">git</span> submodule init <span class="co"># 初始化submodule</span></a>
<a class="sourceLine" id="cb73-5" data-line-number="5"><span class="fu">git</span> submodule update <span class="co"># 更新submodule(必须在根目录执行命令)</span></a>
<a class="sourceLine" id="cb73-6" data-line-number="6"><span class="fu">git</span> submodule update --init --recursive  # 下载的工程带有submodule</a></code></pre></div>
<p>当使用<code>git clone</code>下来的工程中带有submodule时，初始的时候，submodule的内容并不会自动下载下来的，此时，只需执行如下命令：</p>
<div class="sourceCode" id="cb74"><pre><code class="language-bash"><a class="sourceLine" id="cb74-1" data-line-number="1"><span class="fu">git</span> submodule foreach git pull  # submodule 里有其他的 submodule 一次更新</a>
<a class="sourceLine" id="cb74-2" data-line-number="2"><span class="fu">git</span> submodule foreach git pull origin master <span class="co"># submodule更新</span></a>
<a class="sourceLine" id="cb74-3" data-line-number="3"></a>
<a class="sourceLine" id="cb74-4" data-line-number="4"><span class="fu">git</span> submodule foreach --recursive git submodule init</a>
<a class="sourceLine" id="cb74-5" data-line-number="5"><span class="fu">git</span> submodule foreach --recursive git submodule update</a></code></pre></div>
<h2 id="删除文件">删除文件</h2>
<div class="sourceCode" id="cb75"><pre><code class="language-bash"><a class="sourceLine" id="cb75-1" data-line-number="1"><span class="fu">git</span> rm -rf node_modules/</a></code></pre></div>
<h2 id="remote-1">remote</h2>
<p>git是一个分布式代码管理工具，所以可以支持多个仓库，在git里，服务器上的仓库在本地称之为remote。个人开发时，多源用的可能不多，但多源其实非常有用。</p>
<div class="sourceCode" id="cb76"><pre><code class="language-bash"><a class="sourceLine" id="cb76-1" data-line-number="1"><span class="fu">git</span> remote add origin1 git@github.com:yanhaijing/data.js.git  </a>
<a class="sourceLine" id="cb76-2" data-line-number="2"><span class="fu">git</span> remote    # 显示全部源  </a>
<a class="sourceLine" id="cb76-3" data-line-number="3"><span class="fu">git</span> remote -v <span class="co"># 显示全部源+详细信息  </span></a>
<a class="sourceLine" id="cb76-4" data-line-number="4"><span class="fu">git</span> remote rename origin1 origin2 <span class="co"># 重命名  </span></a>
<a class="sourceLine" id="cb76-5" data-line-number="5"><span class="fu">git</span> remote rm origin    # 删除  </a>
<a class="sourceLine" id="cb76-6" data-line-number="6"><span class="fu">git</span> remote show origin  # 查看指定源的全部信息  </a></code></pre></div>
<h2 id="标签tag">标签tag</h2>
<p>当开发到一定阶段时，给程序打标签是非常棒的功能。</p>
<div class="sourceCode" id="cb77"><pre><code class="language-bash"><a class="sourceLine" id="cb77-1" data-line-number="1"><span class="fu">git</span> tag -a v0.1 -m <span class="st">&#39;my version 1.4&#39;</span> <span class="co"># 新建带注释标签   </span></a>
<a class="sourceLine" id="cb77-2" data-line-number="2"><span class="fu">git</span> push origin --tags              # 一次性推送所有分支 </a>
<a class="sourceLine" id="cb77-3" data-line-number="3"><span class="fu">git</span> push origin v1.5                # 推送单个tag到orgin源上 </a>
<a class="sourceLine" id="cb77-4" data-line-number="4"><span class="fu">git</span> tag -v v1.4.2.1                 # 验证标签，验证已经签署的标签</a>
<a class="sourceLine" id="cb77-5" data-line-number="5"><span class="fu">git</span> show v1.5                       # 看到对应的 GPG 签</a>
<a class="sourceLine" id="cb77-6" data-line-number="6"></a>
<a class="sourceLine" id="cb77-7" data-line-number="7"><span class="fu">git</span> tag        # 列出现有标签   </a>
<a class="sourceLine" id="cb77-8" data-line-number="8"><span class="fu">git</span> tag v0gi.1 <span class="co"># 新建标签   </span></a>
<a class="sourceLine" id="cb77-9" data-line-number="9"><span class="fu">git</span> checkout tagname   # 切换到标签       </a>
<a class="sourceLine" id="cb77-10" data-line-number="10"><span class="fu">git</span> tag -d v0.1 <span class="co"># 删除标签   </span></a>
<a class="sourceLine" id="cb77-11" data-line-number="11"><span class="fu">git</span> push origin :refs/tags/v0.1 <span class="co"># 删除远程标签   </span></a>
<a class="sourceLine" id="cb77-12" data-line-number="12"><span class="fu">git</span> pull --all <span class="co"># 获取远程所有内容包括tag  </span></a>
<a class="sourceLine" id="cb77-13" data-line-number="13"><span class="fu">git</span> --git-dir=<span class="st">&#39;&lt;绝对地址&gt;/.git&#39;</span> describe --tags HEAD <span class="co"># 查看本地版本信息  </span></a></code></pre></div>
<h2 id="日志log">日志log</h2>
<div class="sourceCode" id="cb78"><pre><code class="language-bash"><a class="sourceLine" id="cb78-1" data-line-number="1"><span class="fu">git</span> config format.pretty oneline  #显示历史记录时，每个提交的信息只显示一行   </a>
<a class="sourceLine" id="cb78-2" data-line-number="2"><span class="fu">git</span> config color.ui true <span class="co">#彩色的 git 输出   </span></a>
<a class="sourceLine" id="cb78-3" data-line-number="3"><span class="fu">git</span> log <span class="co">#查看最近的提交日志   </span></a>
<a class="sourceLine" id="cb78-4" data-line-number="4"><span class="fu">git</span> log --pretty=oneline <span class="co">#单行显示提交日志   </span></a>
<a class="sourceLine" id="cb78-5" data-line-number="5"><span class="fu">git</span> log --graph --pretty=oneline --abbrev-commit   </a>
<a class="sourceLine" id="cb78-6" data-line-number="6"><span class="fu">git</span> log -num <span class="co">#显示第几条log（倒数）   </span></a>
<a class="sourceLine" id="cb78-7" data-line-number="7"><span class="fu">git</span> reflog <span class="co">#查看所有分支的所有操作记录   </span></a>
<a class="sourceLine" id="cb78-8" data-line-number="8"><span class="fu">git</span> log --since=1.day <span class="co">#一天内的提交；你可以给出各种时间格式，比如说具体的某一天（“2008-01-15”），或者是多久以前（“2 years 1 day 3 minutes ago”）。   </span></a>
<a class="sourceLine" id="cb78-9" data-line-number="9"><span class="fu">git</span> log --pretty=<span class="st">&quot;%h - %s&quot;</span> --author=自己的名字 <span class="co">#查看自己的日志   </span></a>
<a class="sourceLine" id="cb78-10" data-line-number="10"><span class="fu">git</span> log -p -2 <span class="co">#展开两次更新显示每次提交的内容差异   </span></a>
<a class="sourceLine" id="cb78-11" data-line-number="11"><span class="fu">git</span> log --stat <span class="co">#要快速浏览其他协作者提交的更新都作了哪些改动   </span></a>
<a class="sourceLine" id="cb78-12" data-line-number="12"><span class="fu">git</span> log --pretty=format:<span class="st">&quot;%h - %an, %ar : %s&quot;</span>#定制要显示的记录格式   </a>
<a class="sourceLine" id="cb78-13" data-line-number="13"><span class="fu">git</span> log --pretty=format:<span class="st">&#39;%h : %s&#39;</span> --date-order --graph <span class="co"># 拓扑顺序展示   </span></a>
<a class="sourceLine" id="cb78-14" data-line-number="14"><span class="fu">git</span> log --pretty=format:<span class="st">&#39;%h : %s - %ad&#39;</span> --date=short <span class="co">#日期YYYY-MM-DD显示   </span></a>
<a class="sourceLine" id="cb78-15" data-line-number="15"><span class="fu">git</span> log <span class="op">&lt;</span>last tag<span class="op">&gt;</span> HEAD --pretty=format:%s <span class="co"># 只显示commit   </span></a>
<a class="sourceLine" id="cb78-16" data-line-number="16"><span class="fu">git</span> config --global format.pretty <span class="st">&#39;%h : %s - %ad&#39;</span> --date=short <span class="co">#日期YYYY-MM-DD显示 写入全局配置</span></a></code></pre></div>
<table>
<thead>
<tr class="header">
<th>选项</th>
<th>说明</th>
<th>选项</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>%H</td>
<td>提交对象（commit）的完整哈希字串</td>
<td>%ad</td>
<td>作者修订日期（可以用 -date= 选项定制格式）</td>
</tr>
<tr class="even">
<td>%h</td>
<td>提交对象的简短哈希字串</td>
<td>%ar</td>
<td>作者修订日期，按多久以前的方式显示</td>
</tr>
<tr class="odd">
<td>%T</td>
<td>树对象（tree）的完整哈希字串</td>
<td>%cn</td>
<td>提交者(committer)的名字</td>
</tr>
<tr class="even">
<td>%t</td>
<td>树对象的简短哈希字串</td>
<td>%ce</td>
<td>提交者的电子邮件地址</td>
</tr>
<tr class="odd">
<td>%P</td>
<td>父对象（parent）的完整哈希字串</td>
<td>%cd</td>
<td>提交日期</td>
</tr>
<tr class="even">
<td>%p</td>
<td>父对象的简短哈希字串</td>
<td>%cr</td>
<td>提交日期，按多久以前的方式显示</td>
</tr>
<tr class="odd">
<td>%an</td>
<td>作者（author）的名字</td>
<td>%s</td>
<td>提交说明</td>
</tr>
<tr class="even">
<td>%ae</td>
<td>作者的电子邮件地址</td>
<td>-</td>
<td>-</td>
</tr>
</tbody>
</table>
<p><a href="https://git-scm.com/docs/git-log#_pretty_formats">Pretty Formats</a></p>
<h2 id="重写历史">重写历史</h2>
<div class="sourceCode" id="cb79"><pre><code class="language-bash"><a class="sourceLine" id="cb79-1" data-line-number="1"><span class="fu">git</span> commit --amend    # 改变最近一次提交  </a>
<a class="sourceLine" id="cb79-2" data-line-number="2"><span class="fu">git</span> rebase -i HEAD~3  # 修改最近三次的提交说明，或者其中任意一次  </a>
<a class="sourceLine" id="cb79-3" data-line-number="3"><span class="fu">git</span> commit --amend    # 保存好了，这些指示很明确地告诉了你该干什么  </a>
<a class="sourceLine" id="cb79-4" data-line-number="4"><span class="fu">git</span> rebase --continue <span class="co"># 修改提交说明，退出编辑器。  </span></a></code></pre></div>
<div class="sourceCode" id="cb80"><pre><code class="language-bash"><a class="sourceLine" id="cb80-1" data-line-number="1"><span class="ex">pick</span> f7f3f6d changed my name a bit</a>
<a class="sourceLine" id="cb80-2" data-line-number="2"><span class="ex">pick</span> 310154e updated README formatting and added blame</a>
<a class="sourceLine" id="cb80-3" data-line-number="3"><span class="ex">pick</span> a5f4a0d added cat-file</a></code></pre></div>
<p>改成</p>
<pre><code class="language-bash">pick 310154e updated README formatting and added blame
pick f7f3f6d changed my name a bit</code></pre>
<h3 id="删除仓库">删除仓库</h3>
<pre><code class="language-bash">cd ..
rm -rf repo.git</code></pre>
<p><a href="https://help.github.com/articles/changing-author-info/">Github官方教程</a></p>
<h2 id="其它">其它</h2>
<div class="sourceCode" id="cb83"><pre><code class="language-bash"><a class="sourceLine" id="cb83-1" data-line-number="1"><span class="fu">git</span> help *  # 获取命令的帮助信息  </a>
<a class="sourceLine" id="cb83-2" data-line-number="2"><span class="fu">git</span> status  # 获取当前的状态，非常有用，因为git会提示接下来的能做的操作  </a></code></pre></div>
<h2 id="报错问题解决">报错问题解决</h2>
<p><strong>1. <code>git fatal: protocol error: bad line length character: No s</code></strong></p>
<p>解决办法：更换remote地址为 <code>http/https</code> 的</p>
<p><strong>2. <code>The requested URL returned error: 403 Forbidden while accessing</code></strong></p>
<p>解决github push错误的办法：</p>
<div class="sourceCode" id="cb84"><pre><code class="language-bash"><a class="sourceLine" id="cb84-1" data-line-number="1"><span class="co">#vim 编辑器打开 当前项目中的config文件</span></a>
<a class="sourceLine" id="cb84-2" data-line-number="2"><span class="ex">vim</span> .git/config</a>
<a class="sourceLine" id="cb84-3" data-line-number="3"></a>
<a class="sourceLine" id="cb84-4" data-line-number="4"><span class="co">#修改</span></a>
<a class="sourceLine" id="cb84-5" data-line-number="5">[<span class="ex">remote</span> <span class="st">&quot;origin&quot;</span>]  </a>
<a class="sourceLine" id="cb84-6" data-line-number="6">    <span class="ex">url</span> = https://github.com/jaywcjlove/example.git  </a>
<a class="sourceLine" id="cb84-7" data-line-number="7"></a>
<a class="sourceLine" id="cb84-8" data-line-number="8"><span class="co">#为下面代码</span></a>
<a class="sourceLine" id="cb84-9" data-line-number="9">[<span class="ex">remote</span> <span class="st">&quot;origin&quot;</span>]  </a>
<a class="sourceLine" id="cb84-10" data-line-number="10">    <span class="ex">url</span> = https://jaywcjlove@github.com/jaywcjlove/example.git  </a></code></pre></div>
<p><strong>3. git status 显示中文问题</strong></p>
<p>在查看状态的时候 git status 如果是中文就显示下面的情况</p>
<div class="sourceCode" id="cb85"><pre><code class="language-bash"><a class="sourceLine" id="cb85-1" data-line-number="1">\<span class="ex">344</span>\272\247\345\223\201\351\234\200\346\261\202</a></code></pre></div>
<p>解决这个问题方法是：</p>
<div class="sourceCode" id="cb86"><pre><code class="language-bash"><a class="sourceLine" id="cb86-1" data-line-number="1"><span class="fu">git</span> config --global core.quotepath false</a></code></pre></div>
<h2 id="参考资料">参考资料</h2>
<ul>
<li><a href="http://git-scm.com/">Git官网</a></li>
<li><a href="https://try.github.io"><strong>Github 15分钟学习Git</strong></a></li>
<li><a href="http://gitref.org/zh/index.html">Git参考手册</a></li>
<li><a href="http://www.mceiba.com/tool/git-cheat-sheet.html">Git简明手册</a></li>
<li><a href="http://www-cs-students.stanford.edu/~blynn/gitmagic/intl/zh_cn/">Git Magic</a></li>
<li><a href="http://gitbook.liuhui998.com/index.html">Git Community Book 中文版</a></li>
<li><a href="http://git-scm.com/book/en/v2">Pro Git</a></li>
<li><a href="http://marklodato.github.io/visual-git-guide/index-zh-cn.html">图解Git</a></li>
<li><a href="http://rogerdudler.github.io/git-guide/index.zh.html">git-简明指南</a></li>
<li><a href="http://pcottle.github.io/learnGitBranching/">learnGitBranching 在线学习工具</a></li>
<li><a href="http://rogerdudler.github.io/git-guide/index.zh.html">初级教程</a></li>
<li><a href="http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000">廖雪峰的Git教程</a></li>
<li><a href="http://www.worldhello.net/gotgithub/">蒋鑫老师将带你入github的大门</a></li>
<li><a href="http://www.open-open.com/lib/view/open1328069609436.html">git详解</a></li>
<li><a href="http://git.oschina.net/progit/">oschina教程</a></li>
<li><a href="https://github.com/blog/2019-how-to-undo-almost-anything-with-git">How to undo (almost) anything with Git撤销一切，汇总各种回滚撤销的场景，加强学习。</a></li>
<li><a href="http://www.runoob.com/git/git-tutorial.html">Git 教程 | 菜鸟教程runoob.com</a></li>
<li><a href="https://gold.xitu.io/post/5842f9b861ff4b005889ade6">Git 本地仓库和裸仓库</a></li>
<li><a href="http://www.kancloud.cn/kancloud/igit/46710">沉浸式学 Git</a></li>
<li><a href="http://way.oschina.io/2016/12/15/notes/GitAdvance/?utm_source=gank.io&amp;utm_medium=email">Git进阶用法，主要是rebase高级用法</a></li>
</ul>
<!-- Linux命令行搜索引擎：https://jaywcjlove.github.io/linux-command/ -->
